﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HuaCheng.CommonClass;
using HuaCheng.DataAccess;

namespace HuaCheng.Views
{
    public partial class BatteryColor : Form
    {
        //配置config.ini文件地址
        private static string path = DicHelper.RetrieveConfigPath();//配置config.ini文件路径;
        public delegate void ChangeColor();//修改通道显示颜色委托
        public event ChangeColor ChangeColorEventHandler;//修改通道颜色事件


        public BatteryColor()
        {
            InitializeComponent();
            LoadBtnColor();
        }

        /// <summary>
        /// 设置按钮颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <returns></returns>
        private void ChangeBtnColor(object sender, EventArgs e)
        {
            Color cor = new Color();
            Button btn = sender as Button;
            btncolorDialog.Color = btn.BackColor;
            btncolorDialog.ShowDialog();
            btn.BackColor = this.btncolorDialog.Color;
            cor = this.btncolorDialog.Color;
            SaveBtnColor();
        }
        /// <summary>
        /// 设置默认颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDefault_Click(object sender, EventArgs e)
        {
            DialogResult result = new DialogResult();
            if (IndexPage.language == "en-US")
            {
                result = MessageBox.Show("Use default？", "Tip", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            }
            else
            {
                result = MessageBox.Show("是否默认？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            }
            if (result == DialogResult.OK)
            {
                foreach (Control control in this.Controls)
                {
                    if (control is Button)
                    {
                        control.BackColor = SystemColors.ControlLight;
                    }
                }
                //foreach (UserBase ub in this.Controls)
                //{
                //    ub.BackColor = Color.Silver;//设置所有通道默认颜色
                //}
                SaveBtnColor();
            }
        }
        /// <summary>
        /// 颜色设置保存按钮
        /// </summary>
        private void SaveBtnColor()
        {
            try
            {
                foreach (Control control in this.Controls)
                {
                    if (control is Button)
                    {
                        if (control.Name != "btnSubmit")
                        {
                            string color = ((control.BackColor.ToArgb())).ToString("x8");
                            int red = Convert.ToInt32(color.Substring(2, 2), 16);
                            int green = Convert.ToInt32(color.Substring(4, 2), 16);
                            int blue = Convert.ToInt32(color.Substring(6, 2), 16);
                            int colorValue = (65536 * blue) + (256 * green) + red;
                            string strSec = "button color";
                            ReadAndWriteIni.WritePrivateProfileString(strSec, control.Name, Convert.ToString(colorValue), path);
                        }  
                    }
                }
                ChangeColorEventHandler();//触发事件
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace + e.Message);
            }
        }
        /// <summary>
        /// 加载已设置的颜色
        /// </summary>
        private void LoadBtnColor()
        {
            foreach (Control control in this.Controls)
            {
                if (control is Button)
                {
                    try
                    {
                        string buttonname = control.Name;
                        StringBuilder temp = new StringBuilder(500);
                        int i = ReadAndWriteIni.GetPrivateProfileString("button color", buttonname, "", temp, 500, path);
                        int blue = Convert.ToInt32(temp.ToString()) / 65536;
                        int green = (Convert.ToInt32(temp.ToString()) % 65536) / 256;
                        int red = Convert.ToInt32(temp.ToString()) - (65536 * blue) - (256 * green);
                        Color color = Color.FromArgb(255, red, green, blue);
                        control.BackColor = color;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.StackTrace + e.Message);
                    }
                }
            }
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #region 国际化
        private void BatteryColor_Load(object sender, EventArgs e)
        {
            ResourceCulture.SetCurrentCulture(IndexPage.language);
            this.SetResourceCulture();
        }
        /// <summary>
        /// 为当前页面内所有控件设置文本语种
        /// </summary>
        private void SetResourceCulture()
        {
            this.Text = ResourceCulture.GetString("BatteryColorText");
            this.btnCharge.Text = ResourceCulture.GetString("ChargeText");
            this.btnDischarge.Text = ResourceCulture.GetString("DischargeText");
            this.btnShelve.Text = ResourceCulture.GetString("ShelveText");
            this.btnProtect.Text = ResourceCulture.GetString("ProtectText");
            this.btnStop.Text = ResourceCulture.GetString("StopText");
            this.btnPause.Text = ResourceCulture.GetString("PauseText");
            this.btnComplete.Text = ResourceCulture.GetString("CompleteText");
            this.btnDefault.Text = ResourceCulture.GetString("DefaultText");
            this.btnSubmit.Text = ResourceCulture.GetString("ColorSureText");
        }
        #endregion
    }
}
